<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :rules="queryForm" :inline="true" v-show="showSearch"
             label-width="68px">
      <el-form-item label="招商标题" prop="investmentTitle">
        <el-input
          v-model="queryParams.investmentTitle"
          placeholder="请输入招商标题"
          clearable
          size="small"
          maxlength="30"
          show-word-limit
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布人" prop="investmentAuthor">
        <el-input
          v-model="queryParams.investmentAuthor"
          placeholder="请输入发布人"
          clearable
          size="small"
          maxlength="30"
          show-word-limit
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否显示" prop="isDisplay">
        <el-select v-model="queryParams.isDisplay" placeholder="请选择" clearable size="small">
          <el-option
            v-for="dict in displayOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否置顶" prop="isTop">
        <el-select v-model="queryParams.isTop" placeholder="请选择" clearable>
          <el-option
            v-for="dict in topOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="招商类型" prop="investmentType">
        <el-select v-model="queryParams.investmentType" placeholder="请选择招商类型" clearable
                   size="small">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="发布时间" prop="dateRange">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['server:investment:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['server:investment:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['server:investment:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="investmentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center" type="index" prop="investmentId" key="investmentId"
                       v-if="columns[0].visible"/>
      <el-table-column label="招商内容" align="center" prop="investmentDetails" key="investmentDetails"
                       v-if="columns[1].visible" :show-overflow-tooltip='true'>
        <template slot-scope="scope">
          <div style="height: 20px" v-html="scope.row.investmentDetails"></div>
        </template>
      </el-table-column>
      <el-table-column label="招商标题" align="center" prop="investmentTitle" key="investmentTitle"
                       v-if="columns[2].visible" :show-overflow-tooltip='true'/>
      <el-table-column label="发布人" align="center" prop="investmentAuthor" key="investmentAuthor"
                       v-if="columns[3].visible" :show-overflow-tooltip='true'/>
      <el-table-column label="是否显示" align="center" prop="isDisplay" key="isDisplay" v-if="columns[4].visible"
                       :show-overflow-tooltip='true'>
        <template slot-scope="scope">
          <span v-if="scope.row.isDisplay == 0">显示</span>
          <span v-if="scope.row.isDisplay == 1">不显示</span>
        </template>
      </el-table-column>
      <el-table-column label="是否置顶" align="center" prop="isTop" key="isTop" v-if="columns[5].visible"
                       :show-overflow-tooltip='true'>
        <template slot-scope="scope">
          <span v-if="scope.row.isTop == 1">置顶</span>
          <span v-if="scope.row.isTop == 2">不置顶</span>
        </template>
      </el-table-column>
      <el-table-column label="招商类型" align="center" prop="investmentType" :formatter="investmentTypeFormat"
                       key="investmentType" v-if="columns[6].visible" :show-overflow-tooltip='true'/>
      <el-table-column label="发布时间" align="center" prop="investmentDatetime" width="180" key="investmentDatetime"
                       v-if="columns[7].visible" :show-overflow-tooltip='true'>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.investmentDatetime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="点击次数" align="center" prop="contentHit" key="contentHit" v-if="columns[8].visible"
                       :show-overflow-tooltip='true'/>
      <el-table-column label="备注" align="center" prop="remark" key="remark" v-if="columns[9].visible"
                       :show-overflow-tooltip='true'/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['server:investment:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['server:investment:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改运营管理招商引资对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="招商图片" prop="investmentImg">
              <el-upload
                v-if="form.investmentImg"
                ref="upload"
                action=""
                :show-file-list="false"
                list-type="picture-card"
                accept=".jpg,.jpeg,.png,.JPG,.JPEG"
                :auto-upload="false"
                :on-change="getFile">
                <img :src="form.investmentImg" style="width: 148px;height: 148px;border-radius: 6px">
                <el-button slot="trigger" size="small" type="primary">选取图片</el-button>
                <div slot="tip" class="el-upload__tip">只能上传一张jpg/png图片，且不超过500kb</div>
              </el-upload>
              <el-upload
                v-else
                ref="upload"
                action=""
                :show-file-list="true"
                list-type="picture-card"
                accept=".jpg,.jpeg,.png,.JPG,.JPEG"
                :auto-upload="false"
                :on-change="getFile">
                <el-button slot="trigger" size="small" type="primary">选取图片</el-button>
                <div slot="tip" class="el-upload__tip">只能上传一张jpg/png图片，且不超过500kb</div>
              </el-upload>
            </el-form-item>
            <el-form-item label="招商标题" prop="investmentTitle">
              <el-input v-model="form.investmentTitle" placeholder="请输入招商标题" maxlength="200"/>
            </el-form-item>
            <el-form-item label="招商简介" prop="investmentProfile">
              <el-input v-model="form.investmentProfile" type="textarea" placeholder="请输入招商简介" maxlength="500"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发布人" prop="investmentAuthor">
              <el-input v-model="form.investmentAuthor" placeholder="请输入发布人" maxlength="30"/>
            </el-form-item>
            <el-form-item label="是否显示" prop="isDisplay">
              <el-select v-model="form.isDisplay" placeholder="请选择" style="width: 100%">
                <el-option
                  v-for="dict in displayOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="是否置顶" prop="isTop">
              <el-select v-model="form.isTop" placeholder="请选择" style="width: 100%">
                <el-option
                  v-for="dict in topOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="招商类型" prop="investmentType">
              <el-select v-model="form.investmentType" placeholder="请选择招商类型" style="width: 100%">
                <el-option
                  v-for="dict in typeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="发布时间" prop="investmentDatetime">
              <el-date-picker clearable size="small"
                              style="width: 100%"
                              v-model="form.investmentDatetime"
                              type="datetime"
                              value-format="yyyy-MM-dd HH:mm"
                              placeholder="选择发布时间">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" maxlength="200"/>
            </el-form-item>
          </el-col>
          <el-col>
          <el-form-item label="招商内容" prop="investmentDetails">
            <editor v-model="form.investmentDetails" :min-height="192" placeholder="请输入内容"/>
          </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInvestment,
  getInvestment,
  delInvestment,
  addInvestment,
  updateInvestment,
  exportInvestment
} from '@/api/server/operate/investment'
import Editor from "@/components/Editor";

export default {
  name: 'Investment',
  components: {Editor},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 运营管理招商引资表格数据
      investmentList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      typeOptions: [],
      //显示状态字典
      displayOptions: [],
      //置顶状态字典
      topOptions: [],
      //日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        investmentDetails: null,
        investmentTitle: null,
        investmentAuthor: null,
        isDisplay: null,
        isTop: null,
        investmentType: null,
        contentHit: null,
        dateRange: null,
      },
      // 表单参数
      form: {},
      columns: [
        {key: 0, label: "序号", visible: true},
        {key: 1, label: "招商内容", visible: true},
        {key: 2, label: "招商标题", visible: true},
        {key: 3, label: "发布人", visible: true},
        {key: 4, label: "是否显示", visible: true},
        {key: 5, label: "是否置顶", visible: true},
        {key: 6, label: "招商类型", visible: true},
        {key: 7, label: "发布时间", visible: true},
        {key: 8, label: "点击次数", visible: true},
        {key: 9, label: "备注", visible: true},
      ],
      queryForm: {
        investmentAuthor: [{pattern: /^[\u4E00-\u9FA5A-Za-z0-9]+$/, message: '不能为特殊字符', trigger: 'blur'}],
      },
      // 表单校验
      rules: {
        isTop: [{
          required: true, message: '是否置顶不能为空', trigger: 'blur'
        }
        ],
        investmentImg: [{
          required: true,
          message: "招商图片不能为空",
          trigger: "blur"
        }],
        investmentProfile: [{
          required: true,
          message: "招商简介不能为空",
          trigger: "blur"
        }],
        investmentDetails: [{
          required: true,
          message: "招商内容不能为空",
          trigger: "blur"
        }],
        investmentTitle: [{
          required: true,
          message: "招商标题不能为空",
          trigger: "blur"
        }],
        investmentAuthor: [{
          required: true,
          message: "发布人不能为空",
          trigger: "blur"
        }],
        isDisplay: [{
          required: true,
          message: "是否显示不能为空",
          trigger: "change"
        }],
        investmentType: [{
          required: true,
          message: "招商类型不能为空",
          trigger: "change"
        }],
        investmentDatetime: [{
          require: true,
          message: "发布时间不能为空",
          trigger: "blur"
        }],
      },
    }
  },
  created() {
    this.getList()
    this.getDicts('t_investment_type').then(response => {
      this.typeOptions = response.data;
    })
    this.getDicts('t_is_display').then(response => {
      this.displayOptions = response.data;
    })
    this.getDicts('t_is_top').then(response => {
      this.topOptions = response.data;
    })
  },
  methods: {
    /** 查询运营管理招商引资列表 */
    getList() {
      this.loading = true
      listInvestment(this.addDateRange1(this.queryParams, this.dateRange)).then(response => {
        this.investmentList = response.rows
        this.total = response.total
        this.loading = false
      })

    },
    // 状态字典翻译
    investmentTypeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.investmentType)
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        investmentId: null,
        investmentDetails: null,
        investmentTitle: null,
        investmentAuthor: null,
        isDisplay: null,
        isTop: null,
        investmentType: null,
        investmentDatetime: null,
        contentHit: null,
        createBy: null,
        delFlag: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }

      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.$refs['queryForm'].validate(valid => {
        if (valid) {
          this.getList()
        }
      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.queryParams.params.beginTime = ''
      this.queryParams.params.endTime = ''
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.investmentId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    //上传图片
    getFile(file) {
      this.getBase64(file.raw).then(res => {
        this.form.investmentImg = res
      });
    },
    //将本地图片转成base64
    getBase64(file) {
      return new Promise(function (resolve, reject) {
        let reader = new FileReader();
        let imgResult = "";
        reader.readAsDataURL(file);
        reader.onload = function () {
          imgResult = reader.result;
        };
        reader.onerror = function (error) {
          reject(error);
        };
        reader.onloadend = function () {
          resolve(imgResult);
        };
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加运营管理招商引资'
      this.$nextTick(() => {
        this.$refs.upload.clearFiles();
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const investmentId =
        row.investmentId || this.ids
      getInvestment(investmentId).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改运营管理招商引资'
        this.$nextTick(() => {
          this.$refs.upload.clearFiles();
        })
      })

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
          if (valid) {
            if (this.form.investmentId != null) {
              updateInvestment(this.form).then(response => {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })

            } else {
              addInvestment(this.form).then(response => {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })

            }
          }
        }
      )

    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const investmentIds = row.investmentId || this.ids
      this.$confirm('是否确认删除该数据项?', '警告', {
        cancelButtonClass: 'btn-custom-cancel',
        closeOnClickModal: false,
        type: 'warning'
      }).then(function () {
        return delInvestment(investmentIds)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      console.log(this.queryParams)
      this.$confirm('是否确认导出所有运营管理招商引资数据项?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'info'
      }).then(function () {
        return exportInvestment(queryParams)
      }).then(response => {
        this.download(response.msg)
      })
    }
  }
}
</script>
